<?php

namespace app\admin\controller;

use app\admin\model\Con1 as Con1Model;
use app\admin\model\Role;

use think\Controller;
use think\Db;
use think\Request;



class User extends Admin
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public $user = null ;
    public function _initialize()
    {


        parent::_initialize();

        $this->user = new Con1Model();
    }

    public function index()
    {

        $list = Db::table('lamp_user')->field(['id', 'username', 'name'])->order('id', 'ASC')->select();
//        var_dump($list[0]['id']);


        foreach ($list as $k=>$v) {
            $res = Db::table(['lamp_user_role'=>'ur', 'lamp_role'=>'r'])->field(['r.name'])->where('ur.uid = '.$v['id'].' and ur.rid = r.id')->select();
//            var_dump($res);
            $list[$k]['role']=$res;
//            print_r($list);
//            die;
        }


        return view('admin@con1/tables', ['title' => '用户列表',
            'list' => $list
        ]);

    }




    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        return view('admin@con1/func1');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $p =$request->post();
//        var_dump($p);
//        die;
        if ($p['userpass'] != $p['reuserpass'])
        {
            return $this->error('确认密码有误!');
        }
//        echo 111;die;

        $user= new Con1Model();
        $user->data($p);
//        var_dump($user);

        $user->userpass = $user->setPass($p['userpass']);
//        var_dump($user->userpass);
//        die;
        $result = $user->allowField(true)->save();

        if ($result > 0)
        {
            return $this->success('添加成功', url('admin/User/index'));
        } else {
            return $this->error('添加失败!');
        }


    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $list = $this->user->field(['userpass'], true)->find($id);
//        var_dump($list);


        return view ('admin@con1/func2', [
            'list' => $list
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $p=$request->post();
//        var_dump($p);
        $res = $this->user->allowField(true)->save($p, ['id'=>$p['id']]);
        if ($res > 0) {
            return $this->success('更新成功!', url('admin/User/index'));
        } else {
            return $this->error('更新失败!');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $user = new Con1Model();
        $res = $user->where(['id'=>$id])->delete();
        Db::table('lamp_user_role')->where(['uid'=>$id])->delete();
        if ($res > 0)
        {
            $info['status'] = true;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除成功!';
        }else {
            $info['status'] = false;
            $info['id'] = $id;
            $info['info'] = 'ID为: ' . $id . '的用户删除失败!';
        }




        return json($info);
    }

    public function rolediv($id)
    {
//         var_dump($id);
//         通过id 查到用户名
        $old = Db::table(['lamp_user_role'=>'ur', 'lamp_role'=>'r'])->field(['r.id'])->where('ur.uid = '.$id.' and ur.rid = r.id')->select();
//        var_dump($old);die;
        $arr = array();
        foreach ($old as $v)
        {
            $arr[] = $v['id'];
        }


        $name=Db::table('lamp_user')->field('name')->find($id);
//        var_dump($name);die;
        $role=Db::table('lamp_role')->field(['id','name'])->order('id','ASC')->select();
//        var_dump($role);die;
//        var_dump($id);die;

        return view ('admin@con1/func3',[
            'id' =>$id,
            'name' => $name,
            'list' => $role,
            'arr'=>$arr
        ]);
    }

    public function saveRole()
    {

        if(empty($_POST['role'])){
            $this->error("请选择一个角色！");
        }
//        var_dump($_POST['uid']);
//        die;
        $uid = $_POST['uid'];
       //先删掉所有的lamp_user_role 中UID对应的rid
        Db::table('lamp_user_role')->where(['uid'=>$uid])->delete();

        foreach($_POST['role'] as $v)
        {
            $data['uid'] = $uid;
            $data['rid'] = $v;
            Db::table('lamp_user_role')->insert($data);
        }

        $this->success('角色分配成功', url('admin/User/index'));

    }





}
